package model

import (
	"github.com/golang-module/carbon/v2"
	"simcard/bll/upiot"
	"time"
)

type RepaidStatus int

const (
	RepaidStatusDelay RepaidStatus = iota + 1
	RepaidStatusRightNow
	RepaidStatusFinished
)

type RepaidSource int

const (
	RepaidSourceApp RepaidSource = iota + 1
	RepaidSourceWeb
)

type AgentType string

const (
	AgentUPIOT AgentType = "100"
)

func (a AgentType) Name() string {

	if a == AgentUPIOT {
		return "量讯"
	}
	return string(a)
}

func (a AgentType) RemainDay() int {
	if a == AgentUPIOT {
		return 7
	}
	return 0
}

func (a AgentType) NextEffectiveRange(current EffectiveRange) EffectiveRange {

	if a == AgentUPIOT {
		var (
			expiredDate = carbon.ParseByFormat(current.ExpiredDate, "2006-01-02", carbon.PRC)
			next        = EffectiveRange{}
		)

		n := expiredDate.AddDay().StartOfMonth()
		next.EffectiveDate = n.ToDateString()
		next.ExpiredDate = n.AddYear().SubDay().ToDateString()

		return next
	}

	return EffectiveRange{}
}

type EffectiveRange struct {
	EffectiveDate string
	ExpiredDate   string
}

func (er EffectiveRange) ToUnix(aType AgentType) (int64, int64) {

	var (
		effectiveDateDate = carbon.CreateFromStdTime(time.Unix(0, 0))
		expiredDate       = carbon.CreateFromStdTime(time.Unix(0, 0))
	)

	if er.EffectiveDate != "" {
		effectiveDateDate = carbon.ParseByFormat(er.EffectiveDate, "2006-01-02", carbon.PRC)
	}
	if er.EffectiveDate != "" {
		expiredDate = carbon.ParseByFormat(er.ExpiredDate, "2006-01-02", carbon.PRC)
	}

	return effectiveDateDate.Timestamp(), expiredDate.Timestamp()
}

func (er EffectiveRange) DaysToExpiry(aType AgentType) int {

	/*
	   	   now: 2024-07-12 14:13:13.30462481 +0800 CST
	   	   expiredDate: 2024-07-13 00:00:00 +0800 CST
	        expiredDate.DiffAbsInDays(now): 0

	        now: 2024-07-12 00:00:00 +0800 CST
	        expiredDate: 2024-07-13 00:00:00 +0800 CST
	   	   expiredDate.DiffAbsInDays(now): 1

	        now: 2024-07-12 14:13:13.30462481 +0800 CST
	     	  expiredDate: 2024-07-19 00:00:00 +0800 CST
	        expiredDate.DiffAbsInDays(now): 6

	        now: 2024-07-12 00:00:00 +0800 CST
	        expiredDate: 2024-07-19 00:00:00 +0800 CST
	        expiredDate.DiffAbsInDays(now): 7
	*/
	var (
		now         = carbon.Now(carbon.PRC)
		expiredDate = carbon.ParseByFormat(er.ExpiredDate, "2006-01-02", carbon.PRC)
	)

	if aType == AgentUPIOT {
		if now.AddDays(7).Gte(expiredDate) {

		}

		return int(now.DiffAbsInDays(expiredDate))
	}

	return 0
}

func (er EffectiveRange) SecondsToExpiry(aType AgentType) int {

	var (
		now         = carbon.Now(carbon.PRC)
		expiredDate = carbon.ParseByFormat(er.ExpiredDate, "2006-01-02", carbon.PRC)
	)

	if aType == AgentUPIOT {
		if now.AddDays(7).Gte(expiredDate) {

		}

		return int(expiredDate.DiffAbsInDays(now))
	}

	return 0
}

type AgentDetail struct {
	Id   int64  `json:"id"`
	Name string `json:"name"`
	Code string `json:"code"`
}

type CardStatus int

const (
	CardStatusUnknown          CardStatus = iota // 未同步
	CardStatusInValidityPeriod                   // 生效中
	CardStatusExpired                            // 已过期
	CardStatusExpiring                           // 即将到期
	CardStatusGracePeriod                        // 优惠期
)

func NewCardStatus(agentType AgentType, s upiot.UpiotCardStatus) CardStatus {

	var (
		res = CardStatusUnknown
	)

	if agentType == AgentUPIOT {
		data := s
		if data == upiot.CardKeeping || data == upiot.CardStopped || data == upiot.CardPendingDeleted || data == upiot.CardDeleted {
			res = CardStatusExpired
		}

		if data == upiot.CardTesting {
			res = CardStatusGracePeriod
		}

		if data == upiot.CardUsing || data == upiot.CardSilent {
			res = CardStatusInValidityPeriod
		}
	}

	return res
}

type RechargeRecordStatus int

const (
	RechargeRecordQueue RechargeRecordStatus = iota + 1
	RechargeRecordSuccess
	RechargeRecordFailed
)
